﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using InvestmentIntelligence.Files;
using InvestmentIntelligence.SchedulingService.Infrastructure;
using InvestmentIntelligence.TradingData.DataLoaders.LoadDataProcessor;
using InvestmentIntelligence.TradingData.DataLoaders.PostProcessing;
using InvestmentIntelligence.TradingData.DataLoaders.PostProcessing.TradingDataSecurity;
using InvestmentIntelligence.TradingData.DataLoaders.Rimes;
using InvestmentIntelligence.TradingData.Handlers.Handlers.CsvHandlers;
using InvestmentIntelligence.TradingData.Handlers.Mus.FieldsConfiguration;
using InvestmentIntelligence.TradingData.TradingDataModels;

namespace InvestmentIntelligence.TradingData.Handlers.Mus
{
    [Export(typeof(IScheduledTaskFactory))]
    [ExportMetadata("TaskName", "LoadMusCsvPositions")]
    public class CsvMusPositionsHandlerFactory : FolderBasedHandlerFactoryBase, IScheduledTaskFactory
    {
        private readonly ScheduledTaskParameter _positionsFolder = new StringScheduledTaskParameter(
            "positions-dir", "specifies a directory containing position data");

        private readonly ScheduledTaskParameter _rimesUser = new StringScheduledTaskParameter(
           "rimes-user", "specifies a rimes user");

        private readonly ScheduledTaskParameter _rimesPassword = new StringScheduledTaskParameter(
            "rimes-password", "specifies a rimes password");

        private readonly ScheduledTaskParameter _fileMask = new StringScheduledTaskParameter(
            "files-mask", "specifies a mask of position files");

        private readonly ScheduledTaskParameter _dateMask = new StringScheduledTaskParameter(
            "date-mask", "specifies a mask of date field");

        public override IEnumerable<ScheduledTaskParameter> GetParameters()
        {
            yield return _positionsFolder;
            yield return _rimesUser;
            yield return _rimesPassword;
            yield return _fileMask;
            yield return _dateMask;
        }

        public override IScheduledTask CreateTask(CreateTaskParams createTaskParams, IDictionary<ScheduledTaskParameter, string> arguments)
        {
            var pFileds = new PositionFields();
            var securityDataLoader = new SecurityDataLoader(_rimesUser.Get(arguments, ""), _rimesPassword.Get(arguments, ""));
            var dateMask = _dateMask.Get(arguments, "M/d/yyyy");
            var loadDataProcessor = new LoadDataProcessor<TradingDataSecurityDto>(securityDataLoader, InvestmentIntelligence.TradingData.AutomapperConfiguration.Configurations.Rimes.Mappings.TradingDataSecurityDto,
                                                               new List<IDataPostProcessing<TradingDataSecurityDto>>
                                                                      {
                                                                          new RimesPostProcessingCurrencyTranslator()
                                                                      }, TradingDataSecurityDto.TradingDataSecurityDtoComparer);

            var files = FilteredFileReader.GetFiles(_positionsFolder.Get(arguments, ""), _fileMask.Get(arguments, ""), "csv");

            return new CsvImportOriginalPositionsHandler(loadDataProcessor,
               MusBootstrapper.Instance.SecurityResolver, files, pFileds, Mappings.TradingDataOriginalPositionDto, new[] { dateMask });
        }


    }
}